﻿<%@ WebHandler Language="C#" Class="GoalHandler" %>

using System;
using System.Collections.Generic;

using EHSubmit.Logic;
using EHSubmit.Logic.Relationship;
using EHSubmit.Model;
using EHSubmit.Util;

public class GoalHandler : BaseAjaxHandler {
    protected override void Mapping()
    {
        OprationMap["List_Goals"] = "FindGoals";         //列出老师或学生的某个课程布置的相关作业
        OprationMap["List_SubGoals"] = "FindSubGoals";   //列出老师或学生的某个课程布置的相关的主观题作业   --如在批改作业时需要
        
        OprationMap["CountStu"] = "CountStuOfGoal";      //某项作业学生们上交情况的数量统计

        OprationMap["CountUnnormal"] = "CountUnnormalGoalsOfCourse";  //（暂时为某个学生）某个课程下未交的作业及未评分的作业的统计
        OprationMap["ListUnnormal"] = "ListUnnormalGoalsOfCourse";    //列出学生某个课程下未交的作业及未评分的作业

        OprationMap["addObjGoal"] = "AddObjGoal";        //发布客观题作业

        OprationMap["listTchGoal"] = "FindAllGoalsOfTeacher";        //列出老师的所有已布置作业
    }
    
    /// <summary>
    /// combobox中列出的某个课程的相关作业信息
    /// </summary>
    public void FindGoals()
    {
        if (Validate(ERole.Teacher) || Validate(ERole.Student))
        {
            string courseID = Context.Request["courseID"];
            //string tid = CurrentUser.ID;
            GoalManager goalManager = new GoalManager();
            IList<Goal> goalList = goalManager.GetGoalsOfCourse(courseID);
            string jsonGoals = JsonHelper.Serialize(goalList);

            IsSuccess = true;
            int count = goalList.Count;
            ResponseText = "{" + string.Format("count:{0},goals:{1}", count, jsonGoals) + "}";
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }  
    }
    
    /// <summary>
    /// 列出老师或学生的某个课程布置的相关的主观题作业   --如在批改作业时需要
    /// </summary>
    public void FindSubGoals()
    {
        if (Validate(ERole.Teacher))
        {
            string courseID = Context.Request["courseID"];
            //string tid = CurrentUser.ID;
            GoalManager goalManager = new GoalManager();
            IList<Goal> goalList = goalManager.GetSubGoalsOfCourse(courseID);
            string jsonGoals = JsonHelper.Serialize(goalList);

            IsSuccess = true;
            int count = goalList.Count;
            ResponseText = "{" + string.Format("count:{0},goals:{1}", count, jsonGoals) + "}";
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }  
    }

    public void FindAllGoalsOfTeacher()
    {
        if (Validate(ERole.Teacher))
        {
            GoalManager goalManager = new GoalManager();
            IList<Goal> goalList = goalManager.ListGoalsByTeacherId(CurrentUser.ID);

            string jsonGoals = JsonHelper.Serialize(goalList);

            IsSuccess = true;
            int count = goalList.Count;
            ResponseText = "{" + string.Format("count:{0},data:{1}", count, jsonGoals) + "}";
            
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        } 
    }

    /// <summary>
    /// 某项作业学生们上交情况的数量统计
    /// </summary>
    public void CountStuOfGoal()
    {
        if (Validate(ERole.Teacher))
        {
            //------统计应该交作业的学生总人数----------
            string courseID = Context.Request["courseID"].ToString();
            IRelationshipManager rsManager = RelationshipFactory.GetInstance();
            IList<User> students = rsManager.GetStudentsOfCourse(courseID);

            //-----统计已经交了作业的学生人数---------
            HomeworkManager hwManager = new HomeworkManager();
            string goalID = Context.Request["goalID"].ToString();
            int submitedCount = hwManager.GetCountFromTeacher(goalID);


            IsSuccess = true;
            ResponseText = "{success:true,statistics:{" + string.Format("total:{0},submited:{1}", students.Count, submitedCount) + "}}";
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }

    /// <summary>
    /// （暂时为某个学生）某个课程下未交的作业及未评分的作业的统计
    /// </summary>
    public void CountUnnormalGoalsOfCourse()
    {
         if (Validate(ERole.Student))
         {
             string courseID = Context.Request["courseID"].ToString();

             //某学生查询某课程下未交作业时，需要所有已交的作业
             HomeworkManager hwManager = new HomeworkManager();
             IList<Homework> hwList = hwManager.ListFromStudent(CurrentUser.ID);

             //某学生查询某课程下未交作业时，需要该课程ID
             Course course = new Course();
             course.Id = courseID;
             IList<Course> courses = new List<Course>();
             courses.Add(course);
             
             GoalManager goalManager = new GoalManager();
             IList<Goal> goalList = goalManager.GetGoalsOfCourse(courseID);
             IList<Goal> goalUncommittedList = goalManager.GetGoalsOfCourses(courses, hwList);
             IList<Goal> goalUnmarkedList = goalManager.GetGoalsUnmarkedOfCourses(courses, hwList);

             IsSuccess = true;
             string format = "total:{0},Uncommitted:{1},Unmarked:{2}";
             ResponseText = "{success:true,statistics:{" + string.Format(format, goalList.Count, goalUncommittedList.Count,goalUnmarkedList.Count) + "}}";
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }

    /// <summary>
    /// 列出学生某个课程下未交的作业及未评分的作业
    /// </summary>
    public void ListUnnormalGoalsOfCourse()
    {
        if (Validate(ERole.Student))
        {
            string courseID = Context.Request["courseID"].ToString();
             
            //某学生查询某课程下未交作业时，需要所有已交的作业
            HomeworkManager hwManager = new HomeworkManager();
            IList<Homework> hwList = hwManager.ListFromStudent(CurrentUser.ID);

            //某学生查询某课程下未交作业时，需要该课程ID
            Course course = new Course();
            course.Id = courseID;
            IList<Course> courses = new List<Course>();
            courses.Add(course);

            GoalManager goalManager = new GoalManager();
            IList<Goal> goalUncommittedList = goalManager.GetGoalsOfCourses(courses, hwList);
            IList<Goal> goalUnmarkedList = goalManager.GetGoalsUnmarkedOfCourses(courses, hwList);

            string jsonData = null;
            string format = "ID:{0},Title:\"{1}\",PublishTime:{2},DeadLine:{3},Description:\"{4}\"";
            if (goalUncommittedList.Count > 0)
            {
                foreach (Goal goal in goalUncommittedList)
                {
                    jsonData += "{" + string.Format(format,goal.ID,goal.Title,JsonHelper.Serialize(goal.PublishTime),JsonHelper.Serialize(goal.DeadLine),"未交") + "},";
                }
            }

            if (goalUnmarkedList.Count > 0)
            {
                foreach (Goal goal in goalUnmarkedList)
                {
                    jsonData += "{" + string.Format(format,goal.ID,goal.Title,JsonHelper.Serialize(goal.PublishTime),JsonHelper.Serialize(goal.DeadLine),"未评分") + "},";
                }
            }
            jsonData.Remove(jsonData.Length - 1);  //去除结尾的逗号
            
            IsSuccess = true;
            ResponseText = "{goals:[" + jsonData + "]}";
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }

    /// <summary>
    /// 布置客观题作业
    /// </summary>
    public void AddObjGoal()
    {
        if (Validate(ERole.Teacher))
        {
            GoalManager goalManager = new GoalManager();
            Goal goal = new Goal();

            IList<string> qidList = JsonHelper.Deserialize<IList<string>>(Context.Request["qesList"]);
            
            goal.CourseID = Context.Request["courseId"];
            goal.TeacherID = CurrentUser.ID;
            goal.Title = Context.Request["goalTitle"];
            goal.Term = ConfigHelper.GetTerm();
            goal.Description = Context.Request["description"];
            goal.DeadLine = DateTime.ParseExact(Context.Request["deadline"], "yyyy年M月d日", null);
            goal.PublishTime = DateTime.Now;
            goal.LastSubmitTime = DateTime.Now;
            goal.GoalType = 1;

            goalManager.AddObjGoal(goal, qidList);
             
            IsSuccess = true;
            ResponseText = "{success:true}";
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }
}